<template>
    <div class="house-wrapper">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item :to="{path: 'home'}"><i class="el-icon-setting"></i> 首页</el-breadcrumb-item>
                <el-breadcrumb-item>管理员</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <el-table :data="tableData" v-loading.body="loading" border style="width: 100%" ref="multipleTable">
            <el-table-column prop="admin_id" label="ID" width="88"></el-table-column>
            <el-table-column prop="admin_user" label="管理员" sortable></el-table-column>
            <el-table-column prop="login_time" label="上次登陆时间"></el-table-column>
            <el-table-column label="操作" width="180">
                <template scope="scope">
                    <el-button size="small"
                            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button size="small" type="danger"
                            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination">
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page.sync="postData.page"
              :page-sizes="[10, 20, 30, 50]"
              :page-size="postData.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="resultTotal">
            </el-pagination>
        </div>

        <el-dialog title="编辑" :visible.sync="dialogFormVisible">
          <el-form :model="form">
            <el-form-item label="名称" >
              <el-input v-model="form.admin_user" class="input-width"></el-input>
            </el-form-item>
            <el-form-item label="密码" >
              <el-input type="password" v-model="form.admin_pass" class="input-width"></el-input>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="dialogFormSubmit">提交</el-button>
              <!-- <el-button>取消</el-button> -->
            </el-form-item>
          </el-form>
          <!-- <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="dialogFormSubmit = false">确 定</el-button>
          </div> -->
        </el-dialog>
    </div>
</template>

<script>
import Objres from '@/components/common/ObjResult';
let token = localStorage.getItem('admin-token');
export default {
    data () {
        return {
            tableData: null,
            form: {
                admin_id: 0,
                admin_user: '',
                admin_pass: '',
                admin_email: '',
                admin_token: '',
                login_time: 0,
                login_ip: 0,
                create_time: 0
            },
            postData: {
                pageSize: 10,
                page: 1,
                searchValue: '',
                orderBy: '',
                orderType: '',
                pid: 0
            },
            dialogFormVisible: false,
            resultTotal: 0,
            loading: true
        };
    },
    methods: {
        handleSizeChange (val) {
            this.postData.pageSize = val;
            this.getResultByPage();
            // console.log(`每页 ${val} 条`);
        },
        handleCurrentChange (val) {
            this.page = val;
            this.getResultByPage();
            // console.log(`当前页: ${val}`);
        },
        getResultByPage () {
            let self = this;
            self.loading = true;
            this.$http.post(
                self.WEB_CONFIG.weburl + '/admin/user/list?token=' + token,
                self.postData,
                {'emulateJSON': true}
                // {headers: {'Content-Type': 'multipart/form-data'}}
            ).then((response) => {
                self.loading = false;
                let res = response.body;
                // console.log(res);
                if (res.status === Objres.ObjResult.RESULT_OK) {
                    self.tableData = res.data.result;
                    self.resultTotal = parseInt(res.data.total);
                }
            });
        },
        handleAdd (index, row) {
            this.dialogFormVisible = true;
            this.form = {};
        },
        handleEdit (index, row) {
            this.dialogFormVisible = true;
            this.form = row;
        },
        dialogFormSubmit () {
            let self = this;
            this.$http.post(
                self.WEB_CONFIG.weburl + '/admin/user/save?token=' + token,
                self.form,
                {'emulateJSON': true}
            ).then((response) => {
                let res = response.body;
                console.log('res===', res);
                if (res.status === Objres.ObjResult.RESULT_OK) {
                    this.$message({message: '保存成功!', type: 'success'});
                    this.getResultByPage();
                } else {
                    this.$message({message: '保存失败!', type: 'error'});
                }
            });
        }
    },
    created () {
        this.getResultByPage();
    }
};
</script>

<style lang="css">
</style>
